home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9538 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  65 lines

  1. Path: news.huji.ac.il!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Qsort & class - HELP
  4. Message-ID: <1996Mar2.145458.4120@news.huji.ac.il>
  5. From: SHALITIN@VMS.HUJI.AC.IL (shalitin)
  6. Date: 2 Mar 96 14:54:57 GMT
  7. Distribution: world
  8. Organization: The Hebrew University
  9. Nntp-Posting-Host: dial-3-7.slip.huji.ac.il
  10. X-Newsreader: WinVN 0.92.6+
  11.  
  12. Hi,
  13. I am trying to qsort() array of pointers to classes, and I don't succeed.
  14. I have no idea why this is happenning, but I  think the sourse of the problem is in 
  15. the CompareNames() function.  
  16. If anyone can help I would greatly appreaciate it. And let me know what I
  17. am doing wrong...
  18. thanks very much..
  19. Nati
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #define NUMBER_OF_NAMES 3
  25.  
  26. class Names
  27. {
  28.   private:
  29.  
  30.      char Name[10];
  31.  
  32.   public:
  33.  
  34.      void  SetName( char *name )
  35.      {
  36.        strcpy( Name, name );
  37.      }
  38.  
  39.      friend void  SortNames( Names *names[], int size )
  40.      {
  41.        qsort( names, size, sizeof(names[0]), CompareNames );
  42.      }
  43.  
  44.      friend int   CompareNames( const void *name1, const void *name2 )
  45.      {
  46.        return  stricmp( ((Names*)name1)->Name, ((Names*)name2)->Name  );
  47.      }
  48. };
  49.  
  50.  
  51. void  main( void )
  52. {
  53.   Names  *names[NUMBER_OF_NAMES];
  54.   char   *tab_names[] = { "Michael", "Jeff", "Frank" };
  55.  
  56.  
  57.   for ( int i = 0; i < NUMBER_OF_NAMES; i++ ) {
  58.  
  59.      names[i] = new Names;
  60.      names[i]->SetName( tab_names[i] );
  61.   }
  62.  
  63.   SortNames( names , NUMBER_OF_NAMES );
  64. }
  65.